package org.bw.bookstore.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.bw.bookstore.book.mapper.BookDetailedMapper;
import org.bw.bookstore.book.mapper.InventoryTaskMapper;
import org.bw.bookstore.book.mapper.StaffMemberMapper;
import org.bw.bookstore.book.service.InventoryTaskService;
import org.bw.bookstore.common.entity.TbBookDetailed;
import org.bw.bookstore.common.entity.TbInventoryTask;
import org.bw.bookstore.common.entity.TbStaffMember;
import org.bw.bookstore.common.utils.ConstantSet;
import org.bw.bookstore.common.utils.JwtTokenUtils;
import org.bw.bookstore.common.utils.ResponseResult;
import org.bw.bookstore.common.utils.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
*@program: wisdom_bookstore
*@description: 盘点任务接口实现
*@author: 孙冬雪
*@create: 2020-10-22 10:50
*/
@Service
public class InventoryTaskServiceImpl implements InventoryTaskService {

    @Autowired
    private BookDetailedMapper bookDetailedMapper;
    @Autowired
    private InventoryTaskMapper inventoryTaskMapper;
    @Autowired
    private StaffMemberMapper staffMemberMapper;

    @Override
    public ResponseResult markInventoryTask(TbInventoryTask tbInventoryTask) {
        ResponseResult result = ResponseResult.SUCCESS();

        //查询图书详情  判断是否有该类书
        TbBookDetailed tbBookDetailed = bookDetailedMapper.selectOne(new QueryWrapper<TbBookDetailed>()
                .lambda().eq(TbBookDetailed::getCode,tbInventoryTask.getBookDetailedCode()));
        if(tbBookDetailed == null || !tbBookDetailed.getDeleted().equals(0)){
            result.setFail("图书不存在，请先添加新品！或已下架!");
            return result;
        }

        //添加数据
        tbInventoryTask.setCode(StringTools.getRandomString(32));
        tbInventoryTask.setCreateTime(new Date());
        tbInventoryTask.setDeleted(0);
        tbInventoryTask.setFinishStatus(0);
        tbInventoryTask.setInventoryRulesCode("111");

        tbInventoryTask.setInventoryTime(tbInventoryTask.getInventoryTime());
        tbInventoryTask.setBookDetailedCode(tbInventoryTask.getBookDetailedCode());

        inventoryTaskMapper.insert(tbInventoryTask);
        //判断是否添加成功
        if(tbInventoryTask.getId()!=null){
            return result;
        }
        return result.setFail("发布盘点任务失败");
    }

    @Override
    public ResponseResult getInventoryTaskByNoFinish() {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();

        //根据状态=0  查询未完成的盘点任务
        List<TbInventoryTask> tbInventoryTasks = inventoryTaskMapper.getInventoryTaskByNoFinish();

        //判断是否有数据
        if(tbInventoryTasks==null || tbInventoryTasks.size()<=0){
            return result.setFail("暂无数据");
        }

        resultMap.put("tbInventoryTasks",tbInventoryTasks);
        result.setResult(resultMap);
        return result;
    }

    @Override
    public ResponseResult getInventoryTaskByFinish() {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();

        //根据状态=1  查询未完成的盘点任务
        List<TbInventoryTask> tbInventoryTasks = inventoryTaskMapper.getInventoryTaskByFinish();

        //判断是否有数据
        if(tbInventoryTasks==null || tbInventoryTasks.size()<=0){
            return result.setFail("暂无数据");
        }

        resultMap.put("tbInventoryTasks",tbInventoryTasks);
        result.setResult(resultMap);
        return result;
    }

    @Override
    public ResponseResult getInventoryTask() {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();

        //查询全部任务
        List<TbInventoryTask> tbInventoryTasks = inventoryTaskMapper.getInventoryTask();

        //判断是否有数据
        if(tbInventoryTasks==null || tbInventoryTasks.size()<=0){
            return result.setFail("暂无数据");
        }

        resultMap.put("tbInventoryTasks",tbInventoryTasks);
        result.setResult(resultMap);
        return result;
    }

    @Override
    public ResponseResult loginStaffMember(String userName, String userPass) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();
        //查询工作人员
        TbStaffMember tbStaffMember= staffMemberMapper.selectOne(new QueryWrapper<TbStaffMember>()
                .eq("user_name",userName)
                .eq("deleted", ConstantSet.NOT)
                .last("limit 1"));
        //工作人员不存在
        if(tbStaffMember == null) {
            result.setSuccess(false);
            return result.setFail("工作人员不存在");
        }

        //判断密码
        String password = userPass + "." +tbStaffMember.getUserSale();
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!tbStaffMember.getUserPass().equals(md5Password)){
            result.setSuccess(false);
            return result.setFail("工作人员密码不正确");
        }

        //生成token
        String token = JwtTokenUtils.makeToken(tbStaffMember.getId(), tbStaffMember.getUserName(), "tbStaffMember");
        resultMap.put("tbStaffMember", tbStaffMember);
        resultMap.put("token", token);
        result.setResult(resultMap);
        return result;
    }

    //查询全部图书品类
    @Override
    public ResponseResult getBookDetailed() {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>(16);

        List<TbBookDetailed> tbBookDetaileds = bookDetailedMapper.selectList(new QueryWrapper<TbBookDetailed>());
        resultMap.put("tbBookDetailed",tbBookDetaileds);
        result.setResult(resultMap);
        return result;
    }
}
